package org.rust.lang;

import java.util.HashMap;
import java.util.Map;

public class Operator
{
   public enum Type
   {
      SUM, MUL, MINUS, ASSIGN, EQUIVALENT, NOT_EQUIVALENT, GREATER_THAN, LESS_THAN 
   }
   
   private final Type type;
   
   private static Map<String, Operator> lookup = new HashMap<String, Operator>();
   
   static
   {
      lookup.put("+", new Operator(Type.SUM));
      lookup.put("*", new Operator(Type.MUL));
      lookup.put("sum", new Operator(Type.SUM));
      lookup.put("mul", new Operator(Type.MUL));
      lookup.put("-", new Operator(Type.MINUS));
      lookup.put("=", new Operator(Type.ASSIGN));
      lookup.put("==", new Operator(Type.EQUIVALENT));
      lookup.put("!=", new Operator(Type.NOT_EQUIVALENT));
      lookup.put(">", new Operator(Type.GREATER_THAN));
      lookup.put("<", new Operator(Type.LESS_THAN));
   }
   
   public boolean isArithmetic()
   {
      return type == Type.MUL || type == Type.SUM;
   }

     
   
   private Operator(Type type)
   {
      this.type = type;
   }
   
   public Type type()
   {
      return this.type;
   }
   
   public String toString()
   {
      switch (this.type)
      {
      case SUM:
         return "+";

      case MUL:
         return "*";

      case MINUS:
         return "-";

      case ASSIGN:
         return "=";

      case EQUIVALENT:
         return "==";

      case NOT_EQUIVALENT:
         return "!=";

      case GREATER_THAN:
         return ">";

      case LESS_THAN:
         return "<";

      default:
      {
         Assert.fail();
         return null;
      }
      }
      
   }
   
   /**
    * If s represents a valid operator then return that operator
    * otherwise return null
    */
   public static Operator parse(String s)
   {
      return lookup.get(s);
   }
}


